package com.example.cloud_disk.controller.admin;


import cn.mybatis.mp.core.mybatis.mapper.context.Pager;
import com.example.cloud_disk.FileReading.FileTypeChecker;
import com.example.cloud_disk.constant.FileTypeConstant;
import com.example.cloud_disk.dto.*;
import com.example.cloud_disk.entity.AdminUsers;
import com.example.cloud_disk.entity.Users;
import com.example.cloud_disk.pojo.Result;
import com.example.cloud_disk.service.AdminUserService;
import com.example.cloud_disk.service.UsersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URISyntaxException;


/**
 * @author majun
 * @package com.example.cloud_disk.controller.admin
 * @date 2024/10/21 20:31
 */

@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminUserController {

    @Autowired
    private AdminUserService adminUserService;

    @Autowired
    private UsersService usersService;

    @Autowired
    private FileTypeChecker fileTypeChecker;

    /**
     * 管理员登录
     *
     * @param adminUserDTO
     * @return
     */
    @PostMapping("/adminLogin")
    public Result login(@RequestBody AdminUserDTO adminUserDTO) {

        log.info("管理员登录:{}", adminUserDTO);

        String token = adminUserService.AdminLogin(adminUserDTO);

        return Result.success(token);

    }

    /**
     * 管理员账号创建
     *
     * @param adminUserDTO
     * @return
     */
    @PostMapping("/saveAdminUser")
    public Result saveAdminUser(@RequestBody AdminUserDTO adminUserDTO) {

        log.info("管理员创建:{}", adminUserDTO);

        String message = adminUserService.SaveAdminUser(adminUserDTO);

        return Result.success(message);

    }

    /**
     * 单个用户创建
     *
     * @param userDTO
     * @return
     * @throws URISyntaxException
     * @throws IOException
     * @throws InterruptedException
     */
    @PostMapping("/saveUser")
    public Result saveUser(@RequestBody UserDTO userDTO) throws URISyntaxException, IOException, InterruptedException {

        log.info("单个用户创建:{}", userDTO);

        String a = usersService.saveUser(userDTO);

        return Result.success(a);

    }

    /**
     * 通过用户上传的Excel或csv文件批量注册用户
     *
     * @param file
     * @return
     * @throws URISyntaxException
     * @throws IOException
     * @throws InterruptedException
     */
    @PostMapping("/saveUsers")
    public Result saveUsers(@RequestParam("file") MultipartFile file) throws URISyntaxException, IOException, InterruptedException {

        log.info("通过用户上传的Excel或csv文件批量注册用户");

        //判断用户上传文件类型
        String fileType = fileTypeChecker.determineFileType(file);

        if (fileType.equals(FileTypeConstant.CSV) || fileType.equals(FileTypeConstant.Excel)) {

            String a = usersService.saveUsers(file, fileType);

            return Result.success(a);

        }

        return Result.success("文件类型不支持");

    }

    /**
     * 管理员权限修改
     *
     * @param adminPermissionsDTO
     * @return
     */
    @PostMapping("/ModificationPermissions")
    public Result ModificationPermissions(@RequestBody AdminPermissionsDTO adminPermissionsDTO) {

        log.info("管理员权限修改:{}", adminPermissionsDTO);

        String a = adminUserService.ModificationPermissions(adminPermissionsDTO);

        return Result.success(a);

    }

    /**
     * 修改管理员账户状态
     *
     * @param id
     * @return
     */
    @PostMapping("/upDataAdminStatus")
    public Result upDataAdminStatus(Integer id) {

        log.info("修改管理员账户状态：{}", id);

        String a = adminUserService.upDataAdminStatus(id);

        return Result.success(a);

    }

    /**
     * 修改用户账号状态
     *
     * @param id
     * @return
     */
    @PostMapping("/upDataUserStatus")
    public Result upDataUserStatus(Integer id) {

        log.info("修改用户账号状态：{}", id);

        String a = usersService.upDataUserStatus(id);

        return Result.success(a);

    }

    /**
     * 管理员账户分页及模糊查询
     *
     * @param getAdminUsersPageDTO
     * @return
     */
    @PostMapping("/getAdminUsersPage")
    public Result<Pager<AdminUsers>> getAdminUsersPage(@RequestBody GetAdminUsersPageDTO getAdminUsersPageDTO) {

        log.info("管理员账户分页及模糊查询:{}", getAdminUsersPageDTO);

        Pager<AdminUsers> pager = adminUserService.getAdminUsersPage(getAdminUsersPageDTO);

        return Result.success(pager);

    }

    /**
     * 用户账号分页及模糊查询
     *
     * @param getUsersPageDTO
     * @return
     */
    @PostMapping("/getAllUsersPage")
    public Result<Pager<Users>> getAllUsersPage(@RequestBody GetUsersPageDTO getUsersPageDTO) {

        log.info("用户账号分页及模糊查询：{}", getUsersPageDTO);

        Pager<Users> pager = usersService.getAllUsersPage(getUsersPageDTO);

        return Result.success(pager);

    }

    /**
     * 管理员账户删除
     *
     * @param id
     * @return
     */
    @PostMapping("/deleteAdminUser")
    public Result deleteAdminUser(Integer id) {

        log.info("管理员账户删除:{}", id);

        String a = adminUserService.deleteAdminUser(id);

        return Result.success(a);

    }

    /**
     * 用户账户删除
     *
     * @param id
     * @return
     */
    @PostMapping("/deleteUser")
    public Result deleteUser(Integer id) throws IOException, InterruptedException {

        log.info("用户账户删除:{}", id);

        String a = usersService.deleteUser(id);

        return Result.success(a);

    }

    /**
     * 修改管理员账户密码
     *
     * @param putPassword
     * @param token
     * @return
     */
    @PostMapping("/putAdminUserPassword")
    public Result putAdminUserPassword(@RequestBody PutPassword putPassword, @RequestHeader("Authorization") String token) {

        log.info("修改管理员账户密码:{}", putPassword);

        String a = adminUserService.putAdminUserPassword(putPassword, token);

        return Result.success(a);

    }

}
